package jt.example.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

    private CheckBox checkBoxA;
    private CheckBox checkBoxB;
    private CheckBox checkBoxC;
    private CheckBox checkBoxD;

    private RadioGroup optionGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkBoxA = (CheckBox) this.findViewById(R.id.checkboxA);
        checkBoxB = (CheckBox) this.findViewById(R.id.checkboxB);
        checkBoxC = (CheckBox) this.findViewById(R.id.checkboxC);
        checkBoxD = (CheckBox) this.findViewById(R.id.checkboxD);

        checkBoxA.setOnCheckedChangeListener(myCheckBoxCheckChanged);
        checkBoxB.setOnCheckedChangeListener(myCheckBoxCheckChanged);
        checkBoxC.setOnCheckedChangeListener(myCheckBoxCheckChanged);
        checkBoxD.setOnCheckedChangeListener(myCheckBoxCheckChanged);

        optionGroup = (RadioGroup) this.findViewById(R.id.radioButton);

        optionGroup.setOnCheckedChangeListener(myRadioOptionCheckChangedListener);

    }

    private CheckBox.OnCheckedChangeListener myCheckBoxCheckChanged = new CheckBox.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            showCheckResult();
        }
    };

    private RadioGroup.OnCheckedChangeListener myRadioOptionCheckChangedListener = new RadioGroup.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            showCheckResult();
        }
    };

    private void showCheckResult(){
        String result = "";

        if(checkBoxA.isChecked()){
            result  = result + checkBoxA.getText().toString() + "  ";
        }

        if(checkBoxB.isChecked()){
            result  = result + checkBoxB.getText().toString() + "  ";
        }

        if(checkBoxC.isChecked()){
            result  = result + checkBoxC.getText().toString() + "  ";
        }

        if(checkBoxD.isChecked()){
            result  = result + checkBoxD.getText().toString() + "  ";
        }

        int checkedRadioBtnId = optionGroup.getCheckedRadioButtonId();
        if(checkedRadioBtnId >0) {
            RadioButton checkedRadioBtn = (RadioButton) this.findViewById(checkedRadioBtnId);
            result = result + checkedRadioBtn.getText().toString();
        }

        ((TextView)this.findViewById(R.id.labelResult)).setText(result);
    }
}
